package com.yqz.sys.service.impl;

import com.yqz.common.CodeMsg;
import com.yqz.common.Constant;
import com.yqz.common.Result;
import com.yqz.common.base.Form;
import com.yqz.common.base.service.impl.BaseServiceImpl;
import com.yqz.common.exception.BussiException;
import com.yqz.common.util.WebUtil;
import com.yqz.sys.domain.CompUser;
import com.yqz.sys.form.CompUserForm;
import com.yqz.sys.mapper.CompRoleMapper;
import com.yqz.sys.mapper.CompUserMapper;
import com.yqz.sys.mapper.CompUserRoleRefMapper;
import com.yqz.sys.query.CompUserQuery;
import com.yqz.sys.service.CompUserService;
import com.yqz.sys.vo.CompRoleVO;
import com.yqz.sys.vo.CompUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CompUserServiceImpl extends BaseServiceImpl implements CompUserService {

    @Autowired
    private CompUserMapper userMapper;

    @Autowired
    private CompUserRoleRefMapper userRoleRefMapper;

    @Autowired
    private CompRoleMapper roleMapper;

    public CompUserServiceImpl(CompUserMapper userMapper) {
        super(userMapper);
    }

    @Override
    public Result login(String phone, String password) {
        CompUserVO user = userMapper.findOneByPhoneAndPassword(phone,password);
        if(user == null){
            return new Result(CodeMsg.USER_PHONE_PASSWORD_ERROR);
        }
        if (user.getFlag().equals(Constant.USER_FLAG_INVALID)){
            return new Result(CodeMsg.USER_ACCOUNT_INVALID_ERROR);
        }
        WebUtil.saveCurrentCompUser(user);
        return new Result();
    }

    @Override
    public Result queryUserRoles(Integer id) {
        List<Integer> rolesByUId = userRoleRefMapper.findRolesByUId(id);
        return new Result(rolesByUId);
    }

    @Override
    @Transactional
    public Result editUserRoles(Integer uId, List<Integer> rId) {
        try {
            userRoleRefMapper.deleteByUId(uId);
            userRoleRefMapper.batchInsert(uId,rId,WebUtil.getCurrentCompUser().getCompId());
        } catch (Exception e) {
            throw new BussiException(CodeMsg.USER_ROLE_SET_ERROR.CODE,CodeMsg.USER_ROLE_SET_ERROR.MSG);
        }
        return new Result();
    }

    @Override
    @Transactional
    public Result updatePwd(CompUser user, String password, String newPassword) {
        String phone = user.getPhone();
        Integer id = user.getId();
        try {
            CompUserVO us = userMapper.findOneByPhoneAndPassword(phone, password);
            if(us == null){
                return new Result(CodeMsg.USER_PASSWORD_ERROR);
            }
            userMapper.updateByPassword(newPassword,id);
        } catch (Exception e) {
            throw new BussiException(CodeMsg.USER_PASSWORD_MODIFY_ERROR.CODE,CodeMsg.USER_PASSWORD_MODIFY_ERROR.MSG);
        }
        return new Result();
    }

    @Override
    public Result getAccountings() {
        CompRoleVO compRoleVO = roleMapper.selectRoleByName(Constant.ROLE_NAME_ACCOUNTING, WebUtil.getCurrentCompUser().getCompId());
        if(compRoleVO == null){
            return new Result(new ArrayList<>());
        }
        List<CompUserVO> Accountings = userMapper.findAllByRole(compRoleVO.getId(), Constant.USER_FLAG_VALID, WebUtil.getCurrentCompUser().getCompId());
        return new Result(Accountings);
    }

    @Override
    public Result add(Form form) {
        CompUserForm compUserForm = (CompUserForm) form;
        CompUserQuery compUserQuery = new CompUserQuery();
        compUserQuery.setPhone(compUserForm.getPhone());
        List<CompUserVO> compUserVOS = userMapper.findByAll(compUserQuery);
        if(!compUserVOS.isEmpty()){
            return new Result(CodeMsg.USER_PHONE_EXIST_ERROR);
        }
        compUserForm.setCompId(WebUtil.getCurrentCompUser().getCompId());
        compUserForm.setFlag(Constant.DEPT_FLAG_VALID);
        compUserForm.setImg(Constant.DELFAULT_IMG);
        compUserForm.setCreateTime(new Date());
        return super.add(form);
    }

    @Override
    @Transactional
    public Result delete(Integer id) {
        try {
            userRoleRefMapper.deleteByUId(id);
            super.delete(id);
        } catch (Exception e) {
            throw new BussiException(CodeMsg.USER_DELETE_ERROR.CODE,CodeMsg.USER_DELETE_ERROR.MSG);
        }
        return new Result();
    }

}
